using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Sealevel;
using System.Diagnostics;
using System.Configuration;

using StudioOneAPI.Event;

namespace DigitalWebIO
{
    public class SeaLevelManager
    {
        //public static bool _escapePressed = false;
        SeaMAX _smax = null;
        int _currentSlave = 247;  // Default
        string _currentPort = "";

        public bool _doorButtonListener = true;
        private int _ioDevice = 247;
        private int _lockSlot = 1;
        private int _currentError = 0;

        public const int NO_ERROR = 0;
        public const int OPEN_ERROR = 1;
        public const int NO_DEVICES_ERROR = 2;

        public delegate void SessionEventHandler(object sender, SessionStatusEventArgs e);



        // For the web based KioskViewer
        public SeaLevelManager(int iodevice, int lockslot)
        {
            _currentError = NO_ERROR;
            _ioDevice = iodevice;
            _lockSlot = lockslot;

            _smax = new SeaMAX();
            _smax.SDL_Initialize();
            _smax.SDL_SearchForDevices();
            int numDevices = _smax.SDL_DeviceCount();

            if (numDevices > 0)
            {
                if (_smax.SM_Open("SeaDAC Lite 0") != 0)
                {
                    _currentError = OPEN_ERROR;
                    // throw new Exception("Error opening IO device");

                }
                else
                {
                    _currentError = NO_ERROR;
                }

            }
            else
            {
                // Try Again
                _smax.SDL_Initialize();
                _smax.SDL_SearchForDevices();
                if (_smax.SDL_DeviceCount() > 0)
                {
                    if (_smax.SM_Open("SeaDAC Lite 0") != 0)
                    {
                        _currentError = OPEN_ERROR;
                        // throw new Exception("Error opening IO device");

                    }
                    else
                    {
                        _currentError = NO_ERROR;
                    }
                }
                else
                {
                    //throw new Exception("Error opening IO device - Device Missing - Studio One Development Team");
                    _currentError = NO_DEVICES_ERROR;
                }
            }


        }

        public int CurrentError
        {
            get { return _currentError; }
            set { _currentError = value; }
        }

        public bool IsOpen()
        {
            return _smax.IsSeaMAXOpen;
        }


        //Properties
        public int CurrentSlave
        {
            get { return _currentSlave; }
            set {
                  _smax.SM_SelectDevice(value);
                  _currentSlave = value; 
                }
        }


        private int DigitalOn(int slaveId, int slot)
        {
            byte[] data = new byte[1];
            data[0] = 255;

            CurrentSlave = slaveId;

            int result = _smax.SM_WriteDigitalOutputs(slot, 1, data);
            return result;

        }

        private int DigitalOff(int slaveId, int slot)
        {
            byte[] data = new byte[1];
            data[0] = 0;

            CurrentSlave = slaveId;
            int result = _smax.SM_WriteDigitalOutputs(slot, 1, data);
            return result;

        }

        public void UnlockVendingDoor()
        {
            DigitalOn(_ioDevice, _lockSlot);
        }

        public void LockVendingDoor()
        {
            DigitalOff(_ioDevice, _lockSlot);
        }

        public bool IsVendingDoorLocked()
        {
            byte[] b = new byte[1];
            bool lockState = false;

            _smax.SM_ReadDigitalOutputs(0, 4, b);

            if ((int)b[0] == 14)
            {
                lockState = true;
            }
            else if ((int)b[0] == 15)
            {
                lockState = false;
            }

            return lockState;
        }

        public void DigitalInputState(int slaveId, int slot)
        {
            byte[] data = new byte[2];
            data[0] = 0;
            data[1] = 0;
            // Read 8 slots
            _smax.SM_ReadDigitalInputs(0, 8, data);
        }

        public void Close()
        {
            try
            {
                _smax.SM_Close();
                _smax.Dispose();
                _smax = null;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }



    }



}
